﻿(function ($) {
  

    //toFixed方法重写
    Number.prototype.toFixed = function (d) {
        var s = this + "";
        if (!d) d = 0;
        if (s.indexOf(".") == -1) s += ".";
        s += new Array(d + 1).join("0");
        if (new RegExp("^(-|\\+)?(\\d+(\\.\\d{0," + (d + 1) + "})?)\\d*$").test(s)) {
            var s = "0" + RegExp.$2, pm = RegExp.$1, a = RegExp.$3.length, b = true;
            if (a == d + 2) {
                a = s.match(/\d/g);
                if (parseInt(a[a.length - 1]) > 4) {
                    for (var i = a.length - 2; i >= 0; i--) {
                        a[i] = parseInt(a[i]) + 1;
                        if (a[i] == 10) {
                            a[i] = 0;
                            b = i != 1;
                        } else break;
                    }
                }
                s = a.join("").replace(new RegExp("(\\d+)(\\d{" + d + "})\\d$"), "$1.$2");

            } if (b) s = s.substr(1);
            return (pm + s).replace(/\.$/, "");
        } return this + "";

    };
    var _fixJsonDate = function (obj) {
        var key, val;
        for (key in obj) {
            if (obj.hasOwnProperty(key)) {
                val = obj[key];
                switch (typeof val) {
                    case "string":
                        if (val.match(/^\/Date\(([\-\d]+)\)\/$/)) {
                            obj[key] = new Date(parseInt(RegExp.$1, 10));
                        } else if (val.match(/^\d{4}\-\d{1,2}\-\d{1,2}T\d{1,2}:\d{1,2}:\d{1,2}(\.\d+)?$/)) {
                            obj[key] = new Date(val.replace(/\-/g, "/").replace("T", " ").replace(/\.\d+$/, ""));
                        }
                        break;
                    case "object":
                        _fixJsonDate(val);
                        break;
                    default:
                        break;
                }
            }
        }
    };
    var postJson = function (url, params, options) {
        var self = this;
        var optionSetup = {
            type: "POST",
            data: JSON.stringify(params),
            "contentType": "application/json",
            converters: {
                "text json": function (str) {
                    var obj = $.parseJSON(str);
                    _fixJsonDate(obj);
                    return obj;
                }
            }
        };
        var opt = $.extend({}, optionSetup, options);
        return $.ajax(url, opt);
    };

    $.extend({ "postJson": postJson });

    var tabFrame = {
        $: window.top.$,
        tabs: [],
        maxId: 0,
        openTab: function (txt, url) {
            var $ = this.$;
            var id = this.genTabId();
            var iframe = $("<iframe/>").attr({
                'src': url,
                'id': id,
                'style': 'width:100%;height:100%;border:none;',
                'class': 'tab-pane'
            });
            var floag = true;
            //$.each($('iframe'), function () {
            //    if (url === $(this).attr("src")) {
            //        var id = "#" + $(this).attr("id");
            //        $('[href=' + id + ']').click();
            //        floag = false;
            //        return floag;
            //    }
            //});
            if (floag) {
                var tab = $('<li class="guidepost-item"> <a data-toggle="tab" href="#' + id + '">' + txt + '</a> <button type="button" class="close s-close" onclick="$.tabFrame.closeTab(\'#' + id + '\')">×</button> </li>');
                $('#main-tab-nav > li:last').before(tab);
                this.guidepost();
                $('#content-frame').append(iframe);
                tab.find('a').click();
            }
        },
        closeTab: function (id) {
            var tab = $('#main-tab-nav a[href=' + id + ']').parent();
            var isActive = tab.hasClass('active');
            if (isActive) {
                var nTab = tab.next('li');
                if (nTab.is('li:last')) {
                    nTab = tab.prev('li');
                }
                nTab.find('a').tab('show');
            }
            tab.remove();
            this.guidepost();
            this.$('#content-frame iframe' + id).remove();
        },

        currentId: function () {
            var $iframe = this.$("#content-frame iframe[id].tab-pane.active");
            var id = $iframe.attr("id");
            return id;
        },
        setTabTitle: function (id, title) {
            var $tab = this.$('#main-tab-nav a[href=' + id + ']').parent();
            this.$("a[data-toggle=tab]", $tab).html(title);
        },

        genTabId: function () {
            var id = (new Date()).valueOf();
            if (id <= this.maxId) {
                id = this.maxId + 1;
            }
            this.maxId = id;
            return 'tab' + id;
        },

        init: function (menu) {
            var self = this;
            this.$(menu).on('click', 'a.text', function (e) {
                e.preventDefault();
                var target = $(this);
                if (!target.hasClass("disabled")) {
                    var url = target.attr('href');
                    var txt = target.text();
                    self.openTab(txt, url);
                }
            });
            this.$(menu).on('click', 'a.addIcon', function (e) {
                e.preventDefault();
                var target = $(this);
                if (!target.hasClass("disabled")) {
                    var url = target.attr('href');
                    var txt = target.attr('title');
                    self.openTab(txt, url);
                }
            });
        },
        guidepost: function () {
            var $ = this.$;
            var sumWidth = 0;
            $('.guidepost-con .nav').removeAttr("style");
            $('.guidepost-con li').each(function () {
                sumWidth += $(this).width() + 4;
            });
            var guidepost_w = $(window).width() - 77;
            var guidepost_resize = function () {
                guidepost_w = $(window).width() - 77;
                $('.guidepost-con').css("width", guidepost_w + "px");
                $('.guidepost-con .nav').css("width", sumWidth + "px");
                var left_margin = guidepost_w - sumWidth + 24;
                if (sumWidth >= guidepost_w) {
                    $('.guidepost-pa').show();
                    $(".nav-tabs").css("margin-left", left_margin + "px");
                } else {
                    $('.guidepost-pa').hide();
                    $(".nav-tabs").css("margin-left", "0px");
                }
            };
            guidepost_resize();
            $(window).on("resize", guidepost_resize);
            var guide_margin = -50;
            $('#guid_after').click(function () {
                var a = -1 * guide_margin + guidepost_w + 50;
                if (a < sumWidth) {
                    guide_margin -= 50;
                    $(".nav-tabs").css("margin-left", guide_margin + "px");
                }
            });
            $('#guid_pre').click(function () {
                if (guide_margin <= -50) {
                    guide_margin += 50;
                    $(".nav-tabs").css("margin-left", guide_margin + "px");
                }
            });
        }
    };
    $.extend({ "tabFrame": tabFrame });
})(jQuery);
//load  
function Load() {
    $("<div class=\"loading-mask\"></div>").css({ display: "block", width: "100%", height: "auto !important" }).appendTo("body");
    $("<div class=\"loading-mask-msg\"></div>").html("<img src='/assets/img/pagination_loading.gif' class='loadimg' />  正在处理，请稍候。。。").appendTo("body").css({ display: "block", left: ($(document.body).outerWidth(true) - 190) / 2, top: ($(window).height() - 45) / 2 });
}
//display Load    
function dispalyLoad() {
    $(".loading-mask").remove();
    $(".loading-mask-msg").remove();
}